--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    unit_moho_exploiter.lua
--  brief:   Moho exploiter fix
--  author:  Owen Martindell
--
--  Copyright (C) 2008.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:GetInfo()
  return {
    name      = "MohoExploiter",
    desc      = "Moho exploiter fix",
    author    = "TheFatController",
    date      = "11 November, 2008",
    license   = "GNU GPL, v2 or later",
    layer     = 0,
    enabled   = true  --  loaded by default?
  }
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local GetUnitNearestEnemy = Spring.GetUnitNearestEnemy
local GetUnitIsActive = Spring.GetUnitIsActive
local GiveOrderToUnit = Spring.GiveOrderToUnit
local GetMyTeamID = Spring.GetMyTeamID

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local noReclaimList = {}
local UPDATE = 0.5

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local MOHO_EXPLOITER = UnitDefNames["cormexp"].id or nil
local mohoExploiters = {}
local timeCounter = 0
local myTeamID

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local function updateMyUnits()
  myTeamID = GetMyTeamID()
  for _,unitID in ipairs(Spring.GetTeamUnits(myTeamID)) do
    if (select(5, Spring.GetUnitHealth(unitID)) == 1) then 
      widget:UnitFinished(unitID, Spring.GetUnitDefID(unitID), myTeamID)
    end
  end
end

function widget:Initialize()
  if Spring.GetSpectatingState() or Spring.IsReplay() then
    Spring.Echo("MohoExploiter widget disabled for spectators")
    widgetHandler:RemoveWidget()
  end
  if (not MOHO_EXPLOITER) then
    Spring.Echo("MohoExploiter widget disabled, no cormexp found in this mod")
    widgetHandler:RemoveWidget()  
  end
  updateMyUnits()
end

function widget:GameStart()
  myTeamID = GetMyTeamID()
end

function widget:UnitFinished(unitID, unitDefID, unitTeam)
  if (unitDefID == MOHO_EXPLOITER) then mohoExploiters[unitID] = true end
end

function widget:Update(deltaTime)
  if (next(mohoExploiters) == nil) then return false end
  if (timeCounter > UPDATE) then
    timeCounter = 0
    if (GetMyTeamID() ~= myTeamID) then
      mohoExploiters = {}
      updateMyUnits()
    end
    for unitID in pairs(mohoExploiters) do
      if (not GetUnitIsActive(unitID)) and (not GetUnitNearestEnemy(unitID, 650)) then
        GiveOrderToUnit(unitID, CMD.ONOFF, { 1 }, { })
      end
    end
  else
    timeCounter = (timeCounter + deltaTime)
  end
end

function widget:UnitGiven(unitID, unitDefID, unitTeam)
  widget:UnitFinished(unitID, unitDefID, unitTeam)
end

function widget:UnitDestroyed(unitID, unitDefID, unitTeam)
  mohoExploiters[unitID] = nil
end

function widget:UnitTaken(unitID, unitDefID, unitTeam)
  mohoExploiters[unitID] = nil
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------